package cn.jinjimi.app.main.widget.CommonRV;

import android.content.Context;
import android.view.ViewGroup;

import cn.jinjimi.app.main.adapter.recycler.MyBaseRecyclerDataAdapter;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by lforxeverc on 2017/3/15.
 */

public abstract class BaseRvAdapter<T extends IViewData> extends MyBaseRecyclerDataAdapter<T, BaseHolder> {
    private final Context mContext;
    private final IActionListener mListener;
    private Set<BaseHolder> mAllViewHolder = new HashSet<>();

    public BaseRvAdapter(Context mContext, IActionListener l) {
        this.mContext = mContext;
        mListener = l;
    }

    public abstract BaseHolder onCreateViewHolder(int viewType);

    @Override
    public final BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseHolder holder = onCreateViewHolder(viewType);
        mAllViewHolder.add(holder);
        return holder;
    }

    @Override
    public final void onBindViewHolder(BaseHolder holder, int position) {
        if (holder != null)
            holder.setViews(position, getItem(position), mListener);
    }

    @Override
    public final int getItemViewType(int position) {
        return getItem(position).getViewType();
    }

    public Context getContext() {
        return mContext;
    }

    public void updateOnView(int type){
        if(!isEmpty() && !mAllViewHolder.isEmpty()){
            for(BaseHolder h :mAllViewHolder){
                if(h.getViewType() == type){
                    h.updateOnView();
                }
            }
        }
    }
}
